package com.xbfinance.manager.service.impl;

import com.xbfinance.manager.dao.P2pProjectOrderMapper;
import com.xbfinance.manager.dao.P2pProjectOrderMapper2;
import com.xbfinance.manager.pojo.po.P2pProjectOrder;
import com.xbfinance.manager.pojo.po.P2pProjectOrderExample;
import com.xbfinance.manager.pojo.po.P2pUserOrder;
import com.xbfinance.manager.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private P2pProjectOrderMapper orderMapper;

    @Autowired
    private P2pProjectOrderMapper2 orderMapper2;

    @Override
    public P2pProjectOrder selectOrderByOid(Integer oid) {

        P2pProjectOrder order = null;

        try {
            P2pProjectOrderExample example = new P2pProjectOrderExample();

            order = orderMapper.selectByPrimaryKey(oid);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }

        return order;
    }

    @Override
    public Integer deleteOrder(Integer oid) {

        Integer result = null;

        try {

            result = orderMapper.deleteByPrimaryKey(oid);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }

        return result;
    }

    @Override
    public int deleteBatch(List<Integer> ids) {

        int i = 0;

        try {

            P2pProjectOrderExample example = new P2pProjectOrderExample();

            P2pProjectOrderExample.Criteria criteria = example.createCriteria();

            criteria.andOidIn(ids);

            i = orderMapper.deleteByExample(example);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }

        return i;
    }

    @Override
    public List<P2pUserOrder> queryOrder(Integer page, Integer rows, Integer queryOid, Long queryProjectid) {

        List<P2pUserOrder> list = null;

        try {

            Integer offset = (page - 1) * rows;

            Map<String, Object> map = new HashMap<>();

            map.put("offset", offset);
            map.put("rows", rows);
            map.put("queryOid", queryOid);
            map.put("queryProjectid", queryProjectid);

            list = orderMapper2.queryOrder(map);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public int queryCount(Integer queryOid, Long queryProjectid) {
        int count = 0;

        try {

            Map<String, Object> map = new HashMap<>();

            map.put("queryOid", queryOid);
            map.put("queryProjectid", queryProjectid);

            count = orderMapper2.queryCount(map);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return count;
    }

    @Override
    public int editOrder(Integer oid, P2pProjectOrder order) {

        int result =0;

        try {
            P2pProjectOrder porder = new P2pProjectOrder();

            porder.setOid(oid);
            porder.setAward(order.getAward());
            porder.setInterest(order.getInterest());
            porder.setMoney(order.getMoney());
            porder.setProjectid(order.getProjectid());
            porder.setRealmoney(order.getRealmoney());
            porder.setRepayaccount(order.getRepayaccount());
            porder.setRepayyesaccount(order.getRepayyesaccount());
            porder.setUserid(order.getUserid());
            porder.setWaitinterest(order.getWaitinterest());
            porder.setWaitrepay(order.getWaitrepay());
            porder.setYesinterest(order.getYesinterest());

            result = orderMapper.updateByPrimaryKeySelective(porder);

        }catch (Exception e){
            e.printStackTrace();
        }

        return result;
    }
}
